<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">

			<!--     <el-form-item label="分配策略号" prop="strategykey">
            <el-input v-model="dataForm.strategykey" placeholder="分配策略号"></el-input>
          </el-form-item>
          <el-form-item label="行号" prop="line">
            <el-input v-model="dataForm.line" placeholder="行号"></el-input>
          </el-form-item> -->
			<el-form-item label="步骤号" prop="orderline">
				<el-input size="small" v-model="dataForm.orderline" placeholder="步骤号"></el-input>
			</el-form-item>
			<!-- <el-form-item label="分配类型" prop="strategytype">
				<el-input v-model="dataForm.strategytype" placeholder="分配类型"></el-input>
			</el-form-item> -->
			<el-form-item label="是否允许超分配" prop="overallocatepicks">
				<!-- <el-input v-model="dataForm.overallocatepicks" placeholder="是否允许超分配"></el-input> -->
				<el-checkbox size="small" v-model="dataForm.overallocatepicks"></el-checkbox>
			</el-form-item>
			<el-form-item label="计量单位" prop="uomlevel">
				<!-- <el-input v-model="dataForm.uomlevel" placeholder="计量单位"></el-input> -->
					<el-select size="small" v-model="dataForm.uomlevel">
						<el-option label="--" value=""></el-option>
						<el-option v-for="item in uomList" :key="item.list" :label="item.listname" :value="item.list">
						</el-option>
					</el-select>

			</el-form-item>
			<el-form-item label="库位类型" prop="locationtype">
				<!-- <el-input v-model="dataForm.locationtype" placeholder="库位类型"></el-input> -->
					<el-select size="small" v-model="dataForm.locationtype">
						<el-option label="--" value=""></el-option>
						<el-option v-for="item in locTypeList" :key="item.list" :label="item.listname"
							:value="item.list">
						</el-option>
					</el-select>
			</el-form-item>
			<el-form-item label="库位类别" prop="locationcategory">
				<!-- <el-input v-model="dataForm.locationcategory" placeholder="库位类别"></el-input> -->
				<el-select size="small" v-model="dataForm.locationcategory">
					<el-option label="--" value=""></el-option>
					<el-option v-for="item in locCategoryList" :key="item.list" :label="item.listname"
						:value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="库位ABC" prop="locationabc">
				<el-input v-model="dataForm.locationabc" placeholder="库位ABC"></el-input>
			</el-form-item> -->
			<el-form-item label="分配区" prop="allocationzone">
				<!-- <el-input v-model="dataForm.allocationzone" placeholder="分配区"></el-input> -->
				<el-select size="small" v-model="dataForm.allocationzone">
					<el-option label="--" value=""></el-option>
					<el-option v-for="item in zoneList" :key="item.zonecode" :label="item.zonename"
						:value="item.zonecode">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="按计量单位和库位创建一项拣货任务" prop="groupuompicks">
				<!-- <el-input v-model="dataForm.groupuompicks" placeholder="按计量单位和库位创建一项拣货任务"></el-input> -->
				<el-checkbox size="small" v-model="dataForm.groupuompicks"></el-checkbox>
			</el-form-item>
			<!-- <el-form-item label="托盘数量" prop="lpnqtyconsidered">
				<el-input v-model="dataForm.lpnqtyconsidered" placeholder="托盘数量"></el-input>
			</el-form-item> -->
			<el-form-item label="分类1" prop="sort1">
				<!-- <el-input v-model="dataForm.sort1" placeholder="分类1"></el-input> -->
				<el-select size="small" v-model="dataForm.sort1">
					<el-option label="--" value=""></el-option>
					<el-option v-for="item in sortList" :key="item.list" :label="item.listname"
						:value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="分类2" prop="sort2">
				<!-- <el-input v-model="dataForm.sort2" placeholder="分类2"></el-input> -->
				<el-select size="small" v-model="dataForm.sort2">
					<el-option label="--" value=""></el-option>
					<el-option v-for="item in sortList" :key="item.list" :label="item.listname"
						:value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="分类3" prop="sort3">
				<!-- <el-input v-model="dataForm.sort3" placeholder="分类3"></el-input> -->
				<el-select size="small" v-model="dataForm.sort3">
					<el-option label="--" value=""></el-option>
					<el-option v-for="item in sortList" :key="item.list" :label="item.listname"
						:value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="分类4" prop="sort4">
				<!-- <el-input v-model="dataForm.sort4" placeholder="分类4"></el-input> -->
				<el-select size="small" v-model="dataForm.sort3">
					<el-option label="--" value=""></el-option>
					<el-option v-for="item in sortList" :key="item.list" :label="item.listname"
						:value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="托盘数量分类规则" prop="lpnquantitysorttype">
				<el-input v-model="dataForm.lpnquantitysorttype" placeholder="托盘数量分类规则"></el-input>
			</el-form-item> -->
			<!-- <el-form-item label="创建人" prop="adduser">
            <el-input v-model="dataForm.adduser" placeholder="创建人"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" prop="addtime">
            <el-input v-model="dataForm.addtime" placeholder="创建时间"></el-input>
          </el-form-item>
          <el-form-item label="更新人" prop="updatuser">
            <el-input v-model="dataForm.updatuser" placeholder="更新人"></el-input>
          </el-form-item>
          <el-form-item label="更新时间" prop="updatetime">
            <el-input v-model="dataForm.updatetime" placeholder="更新时间"></el-input>
          </el-form-item> -->
		</el-form>
		<template #footer>
			<el-button size="small" @click="visible = false">取消</el-button>
			<el-button size="small" type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getAllocationstrategydetailById,
		putAllocationstrategydetail,
		postAllocationstrategydetail
	} from "@/network/lswms/base/Allocationstrategydetail"
	import {
		ElMessage
	} from 'element-plus'
	import {
		getCodeListByCode
	} from "@/network/lswms/base/Codelist.js"
	import {
		getzonelist
	} from "@/network/lswms/base/Zone.js"
	import _ from 'lodash'


	export default {
		name: "allocationstrategydetailAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				strategykey: '',
				line: '',
				orderline: '',
				strategytype: '',
				overallocatepicks: false,
				uomlevel: '',
				locationtype: '',
				locationcategory: '',
				locationabc: '',
				allocationzone: '',
				groupuompicks: false,
				lpnqtyconsidered: '',
				sort1: '',
				sort2: '',
				sort3: '',
				sort4: '',
				lpnquantitysorttype: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: ''
			});


			const dataRule = reactive({
				/*  strategykey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          line: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ], */
				orderline: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				/*     strategytype: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          overallocatepicks: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          uomlevel: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          locationtype: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          locationcategory: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          locationabc: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          allocationzone: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          groupuompicks: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          lpnqtyconsidered: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          sort1: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          sort2: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          sort3: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          sort4: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          lpnquantitysorttype: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          adduser: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          addtime: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          updatuser: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ],          updatetime: [
				       { required: true, message: '必填项不能为空', trigger: 'blur' }
				     ]  */
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getallocationstrategydetailInfo();
					}
					locTypeFn();
					locCategoryFn();
					getZoneListFn();
					uomFn();
					sortFn();
				})
			}

			const getallocationstrategydetailInfo = () => {
				getAllocationstrategydetailById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}




			//获取库位类型
			const locTypeList = ref("");
			const locTypeFn = () => {
				getCodeListByCode('loc_type').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					locTypeList.value = res.data;
				}).catch((err) => {});
			}
			//获取库位类别
			const locCategoryList = ref("");
			const locCategoryFn = () => {
				getCodeListByCode('loc_category').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					locCategoryList.value = res.data;
				}).catch((err) => {});
			}

			const uomList = ref("");
			const uomFn = () => {
				getCodeListByCode('UOMLEVEL').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					uomList.value = res.data;
				}).catch((err) => {});
			}
			
			const sortList = ref("");
			const sortFn = () => {
				getCodeListByCode('STRATEGYSORT').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					sortList.value = res.data;
				}).catch((err) => {});
			}

			const zoneList = ref({});
			const getZoneListFn = () => {
				getzonelist().then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					zoneList.value = res.data;
				}).catch((err) => {});
			}




			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}

					let qing = () => {
						return !dataForm.value.id ? postAllocationstrategydetail(dataForm.value) :
							putAllocationstrategydetail(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef,
				zoneList,
				locTypeList,
				locCategoryList,
				uomList,
				sortList

			}
		},


	}
</script>

<style scoped>

</style>
